import { AstContext } from "../context"
import { AstVisitor } from "../visitor"

export class CloseRoomVisitor extends AstVisitor {
    visitTextView(node: AutoJs.UiObject, ctx: AstContext) {
        const text = node.text()
        if (text) {
            if (["退出直播间", "退出"].includes(text)) {
                const clickable = ctx.findInParent(node, it => it.clickable())
                if (clickable) ctx.set('sureCloseRoom', () => {
                    clickable.click()
                    clickable.recycle()
                })
                else ctx.set('sureCloseRoom', () => back())
            }
            if (ctx.idEndWidths(node, 'status_text_text')) {
                back()
            }
            if (text.startsWith('手慢了')) {
                back()
            }
            if (text.startsWith('很遗憾')) {
                back()
            }
            if (text.startsWith('直播已结束')) {
                back()
            }
        }
        return false;
    }
}

export const closeRoomVisitor = new CloseRoomVisitor()